# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo


class NewscrawlerPipeline(object):
    def process_item(self, item, spider):
        return item


class StockPipeline(object):
    def __init__(self):
        self.client = pymongo.MongoClient()

    def process_item(self, item, spider):
        d = dict(item)
        market = d.pop('market')
        self.client[market]['stocklist'].update({'symbol': d['symbol']}, d, upsert=True)
        return item


class HSNewsPipeline(object):
    def __init__(self):
        self.client = pymongo.MongoClient()
        self.db = self.client.hs

    def process_item(self, item, spider):
        d = dict(item)
        symbol = d.pop('symbol')
        self.db[symbol].update({'title': d['title']}, d, upsert=True)


class HKNewsPipeline(object):
    def __init__(self):
        self.client = pymongo.MongoClient()
        self.db = self.client.hk

    def process_item(self, item, spider):
        d = dict(item)
        symbol = d.pop('symbol')
        self.db[symbol].update({'title': d['title']}, d, upsert=True)
